//生成题目 判断正误方法

//开始作答的方法

function startTest(res) {
    console.log(res)
    for (let i = 1; i <= res.qnums; i++) {
        document.getElementsByClassName('question_collec')[0].innerHTML += '<a class="btn btn-primary" href="#/exer' + i + '">' + i + '</a>'
        if (i == res.qnums) {
            document.getElementById('div-mid').innerHTML += '<div class="animated bounceIn question" data-index="' + i + '">(' + i + '.) <span>' + res.questions[i - 1].expressstr + '</span>=<input class="form-control"  style="width:180px; height:60px; font-size: 60px; display: inline-block;"  onkeyup="if(event.keyCode==13){subHandle(event,res)}" type="text"/><button class="btn btn-primary btn-block" onclick="subHandle(event,res)">完成</button></div>';
        } else {
            document.getElementById('div-mid').innerHTML += '<div class="animated bounceIn question" data-index="' + i + '">(' + i + '.) <span>' + res.questions[i - 1].expressstr + '</span>=<input class="form-control" style="width:180px; height:60px; font-size: 60px; display: inline-block;"  onkeyup="if(event.keyCode==13){checkHandle(event,res)}" type="text"/><button class="btn btn-primary btn-block" onclick="checkHandle(event, res)">下一题</button></div>';
        }

        var path = '/exer' + i;
        spaRouters.map(path, function (transition) {
            spaRouters.syncFun(function (transition) {
                $('div [data-index="' + i + '"]').siblings().css('display', 'none');
                $('div [data-index="' + i + '"]').css('display', 'block');
                $('div [data-index="' + i + '"] input').focus();
            }, transition)
        })
    }

    spaRouters.beforeEach(function (transition) {
//		console.log('切换之前dosomething');
        transition.next();
    })
    spaRouters.afterEach(function (transition) {
//		console.log("切换之后dosomething");
    })
    spaRouters.init();
    location.hash = '#/exer1'
    timer.startTimer();
}


//判断对错方法
function checkHandle(event, res) {
    console.log(event)
    var ev = event || window.event;
    var par = $(ev.target).parent();
    index = par.data('index');
    console.log(index);
    rightas = res.questions[index - 1].answer;
    console.log();
    //将÷和×两个符号替换为/和*
    var ques = ($(ev.target).parent().find('span').text()).replace(/÷/g, '/').replace(/×/g, '*');
    var ans = $(ev.target).parent().find('input').val();
    $(ev.target).parent().find('input').val(ans);
    $(ev.target).parent().find('input').attr('disabled', '');
    if (ans == '') {
        wrongFun(ev);
    } else if (ans.indexOf('\'') != -1 && ans.indexOf('/') != -1) {
        var arr = ans.split('\'');
        var result = parseInt(arr[0]) + eval(arr[1]);
        if ((+result).toFixed(2) == (+eval(rightas)).toFixed(2)) {
            rightFun(ev);
        } else {
            wrongFun(ev);
        }

    } else if (ans.indexOf('/') != -1) {
        var arr = ans.split('/');
//		console.log(arr[0]);
//		console.log(arr[1]);
//		console.log((+eval(ans)).toFixed(2))
//		console.log((+eval(ques)).toFixed(2))
//		var ansNumber = (+eval(ans)).toFixed(2);
//		var quesNumber = (+eval(ques)).toFixed(2);
//		console.log(ansNumber-quesNumber)
        console.log(arr[0] + ',' + arr[1])
        if (arr[0] - arr[1] > 0) {
            console.log(1)
            wrongFun(ev);
        } else if ((+eval(ques)).toFixed(2) == (+eval(rightas)).toFixed(2)) {
            console.log(2)
            rightFun(ev);
        } else {
            console.log(3)
            wrongFun(ev);
        }
    } else {
        if ((+ans).toFixed(2) == (+eval(rightas)).toFixed(2)) {
            rightFun(ev);
        } else {
            wrongFun(ev);
        }
    }
    location.hash = '#/exer' + ($(ev.target).parent().data('index') + 1);
}

var custom = [
    'bangTidy',
    'blackgloss'
];

//作答完成提交方法
function subHandle(event, res) {
    var ev = event || window.event;
    checkHandle(ev, res);
    var totalNum = parseInt($('#question_info .wrong_num span').text()) + parseInt($('#question_info .right_num span').text());
    if (totalNum == $('.question_collec a').length) {
        var rightans = $('.question_collec a[class="btn btn-success"]').length;
        var subData = {
            examid: res.examid,
            time: 0,
            accuracy: 0,
            rightans: rightans,
        };
        timer.stopTimer();
        subData.time = (+timer.sec) + (+timer.min) * 60 + (+timer.hou) * 3600
        subData.accuracy = ($('.question_collec a[class="btn btn-success"]').length / totalNum * 100).toFixed(2);
        //提交完成题目的信息
        $.post('/class/exam/finish', subData, function (res) {
            if (res.success) {
                console.log('提交成功');
                $(ev.target).remove();
            }
        });
        $('#div-mid input').removeAttr('disabled');
        $('.rightnums').text(rightans);
        $('.rightrate').text(subData.accuracy);
        $('.sec').text(timer.sec);
        $('.hour').text(timer.hou);
        $('.min').text(timer.min);
        $('#ansresult').css('display', 'block');


    } else {
        $('.top-right').notify({
            message: "还没答完呢！请继续做题",
            type: custom[1],
        }).show();
    }
}

//正确行为
function rightFun(event) {
    var ev = event || window.event;
    $('.question_collec a[href="#/exer' + $(ev.target).parent().data('index') + '"]').removeClass('btn-primary').removeClass('btn-danger').addClass('btn-success');
    $('#question_info .right_num span').text($('.question_collec a[class="btn btn-success"]').length);
    $('#question_info .right_num').addClass('rubberBand-right');
    setTimeout(function () {
        $('#question_info .right_num').removeClass('rubberBand-right');
    }, 400)
}

//错误行为
function wrongFun(event) {
    var ev = event || window.event;
    $('.question_collec a[href="#/exer' + $(ev.target).parent().data('index') + '"]').removeClass('btn-primary').removeClass('btn-success').addClass('btn-danger');
    $('#question_info .wrong_num span').text($('.question_collec a[class="btn btn-danger"]').length);
    $('#question_info .wrong_num').addClass('rubberBand-wrong');
    setTimeout(function () {
        $('#question_info .wrong_num').removeClass('rubberBand-wrong');
    }, 400)
}